#include <jni.h>
#include <stdio.h>
#include <string.h>
#include <android/log.h>


#define DEBUG_TAG "NDK_AndroidNDK1SampleActivity"


//struct raii_class
//{
//	raii_class()
//	{
//		__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", "raii_class ctor");
//	}
//
//	~raii_class()
//	{
//		__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", "raii_class dtor");
//	}
//
//};




//extern "C"
void Java_com_mamlambo_sample_ndk1_AndroidNDK1SampleActivity_helloLog(JNIEnv * env, jobject this_, jstring logThis)
{
	jboolean isCopy;

	//TODO: improve adquire and release with RAII idiom (1)
	const char * szLogThis = (*env)->GetStringUTFChars(env, logThis, &isCopy);
//	const char * szLogThis = env->GetStringUTFChars(logThis, &isCopy);

	__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", szLogThis); //resource usage

	(*env)->ReleaseStringUTFChars(env, logThis, szLogThis); // (1)
//	env->ReleaseStringUTFChars(logThis, szLogThis); // (1)
}

//extern "C"
jstring Java_com_mamlambo_sample_ndk1_AndroidNDK1SampleActivity_getString(JNIEnv * env, jobject this_, jint value1, jint value2)
{
    const char *szFormat = "The sum of the two numbers is: %i";
    char *szResult;

    // add the two values
    jlong sum = value1+value2;

    // malloc room for the resulting string
    szResult = (char*)malloc(sizeof(szFormat) + 20);

    // standard sprintf
    sprintf(szResult, szFormat, sum);

    // get an object string
    jstring result = (*env)->NewStringUTF(env, szResult);
//    jstring result = env->NewStringUTF(szResult);

    // cleanup
    free(szResult);

    return result;
}



//extern "C"
//void Java_com_mamlambo_sample_ndk1_AndroidNDK1SampleActivity_testRAII(JNIEnv * env, jobject this_)
//{
//}




